# Maintainer: Filipe Laíns (FFY00) <lains@archlinux.org>

pkgbase=srslte
pkgname=($pkgbase $pkgbase-avx2)
_pkgname=srsRAN_4G
pkgver=23.04.1
_pkgver=release_${pkgver//./_}
pkgrel=3
pkgdesc='Open source SDR LTE software suite from Software Radio Systems (SRS)'
arch=('loong64' 'x86_64')
url='https://www.srslte.com'
license=('AGPL3')
depends=('fftw' 'mbedtls' 'boost-libs' 'lksctp-tools' 'libconfig'
         'libuhd' 'soapysdr' 'bladerf' 'zeromq' 'pcsclite' 'srsgui')
makedepends=('cmake' 'boost')
provides=('srsue' 'srsenb' 'srsepc')
conflicts=('srsue' 'srsenb' 'srsepc')
source=("$_pkgname-$pkgver.tar.gz::https://github.com/srsran/srsRAN/archive/$_pkgver.tar.gz"
         string.patch)
sha512sums=('725a7d4a317d913d52f10b411cdf647401bcc688ac82f215231b422696b460e16f5d5173e1336be5b7744a338e89eca2293b65689120dab7598bf32bff782f74'
            '8cd2ceeb8e15480aa8390e70e841bbe9fc8b340e7a19beadb2ece4a9159741a4f1cb8ab6e1b72e77c73aca13d1a4b569e7ca1a8658e71d64ce9c04540dd384e4')
options=(!lto)

prepare() {
  cd $_pkgname-$_pkgver

  # Using too old of a version to even attempt to properly fix these
  sed -e '/-Werror\"/d' -i CMakeLists.txt
  CXXFLAGS+=' -Wno-error -Wp,-U_GLIBCXX_ASSERTIONS'

  # Disable failing test
  sed -i '/add_test(ue_rf_failure/d' srsue/src/CMakeLists.txt

  mkdir build

  patch -p1 < "$srcdir"/string.patch # Add missing include

  cp -r "$srcdir"/$_pkgname-$_pkgver "$srcdir"/$_pkgname-avx2-$_pkgver
}

build() {
  echo 'Building srslte...'
  cd "$srcdir"/$_pkgname-$_pkgver/build

  cmake .. \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_GENERIC=True

  make

  echo 'Building srslte-avx2...'
  cd "$srcdir"/$_pkgname-avx2-$_pkgver/build

  cmake .. \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_GENERIC=True \
    -DHAVE_AVX2=True

  make
}

check() {
  echo 'Running tests for srslte...'
  cd "$srcdir"/$_pkgname-$_pkgver/build

# network_utils_test fails on build server
  ctest $(echo $MAKEFLAGS | grep -Po '\-j[0-9][0-9]*') -E network_utils_test

  echo 'Running tests for srslte-avx2...'
  cd "$srcdir"/$_pkgname-avx2-$_pkgver/build

  ctest $(echo $MAKEFLAGS | grep -Po '\-j[0-9][0-9]*') -E network_utils_test
}

package_srslte() {
  install=$pkgname.install

  cd $_pkgname-$_pkgver/build

  make DESTDIR="$pkgdir" install
}

package_srslte-avx2() {
  pkgdesc="$pkgdesc (with AVX2 extensions)"
  provides=($pkgbase)
  conflicts=($pkgbase)
  install=$pkgname.install

  cd $_pkgname-avx2-$_pkgver/build

  make DESTDIR="$pkgdir" install
}
